blog

Home / DeveloperSection / Blogs / Dynamic memory allocation in C++

Dynamic memory allocation in C++

priyanka kushwaha2287 05-Feb-2015

Dynamic memory allocation in C++

In this blog, I’m explaining about Dynamic memory allocation in C++

Allocation:

1. Dynamic memory allocation is the allocation of memory at “run time”.

2. Dynamically allocated memory is kept on the memory heap.

3. Dynamically allocated memory cannot have a “name” it must referred to

4. The new operator is used to dynamically allocated memory. 


De-Allocation


1.      De-allocation is the “clean-up” of space being used for variable or other


data storage.


2.      To de-allocation dynamic memory, we use the delete operator.

 

Example:
// DynamicMemoryAllocation.cpp : main project file.
//Create Dynamic Array
#include"stdafx.h"
#include<iostream>
usingnamespace std;
usingnamespace System;
 
int main(array<System::String ^> ^args)
{
            double* ptr=newdouble;
            int j=0;
            cout<<"\nYou want to terminate press '-1'\n";
            while(*(ptr+j)!='Q')
            {
               cout<<"Enter a number";
                        cin>>*(ptr+j);
                        if(*(ptr+j)==-1)
                                    break;
                        j++;
            }
            for(int i=0;i<j;i++)
                        cout<<*(ptr+i);
       delete ptr;
            Console::ReadKey(0);
    return 0;}

Updated 05-Feb-2015

Leave Comment

Comments

Liked By